<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="客户姓名" prop="customerName">
        <el-input
          v-model="queryParams.customerName"
          placeholder="请输入客户姓名"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="司机姓名" prop="driverName">
        <el-input
          v-model="queryParams.driverName"
          placeholder="请输入司机姓名"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="车牌号" prop="licensePlateNumber">
        <el-input
          v-model="queryParams.licensePlateNumber"
          placeholder="请输入车牌号"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单状态" prop="orderStatus">
        <el-select v-model="queryParams.orderStatus" @change="dataScopeSelectChange">
            <el-option
              v-for="item in dict.type.didi_order_status"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:role:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:role:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:role:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="订单编号" prop="id" width="120" />
      <el-table-column label="客户姓名" prop="tuserDo.userName" :show-overflow-tooltip="true" width="150" />
      <el-table-column label="司机姓名" prop="tdriverDo.driverName" :show-overflow-tooltip="true" width="150" />
      <el-table-column label="车牌号" prop="tcarDo.licensePlateNumber" width="150" />
      <el-table-column label="订单状态"  width="150" >
      <template slot-scope="scope">
        <dict-tag
          :options="dict.type.didi_order_status"
          :value="scope.row.orderStatus"
          :style="{ color: getOrderStatusColor(scope.row.orderStatus) }"
        />
      </template>
    </el-table-column>
      <el-table-column label="实付金额"  prop="actualPayment" width="150"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleOrderStatusChange(scope.row)"
            v-hasPermi="['system:role:edit']"
            v-if="scope.row.orderStatus!=5 "
          >取消订单</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-info"
            @click="OrderDetails(scope.row)"
            v-hasPermi="['system:role:remove']"
          >订单详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改订单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="用户ID" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户ID" />
        </el-form-item>
        <el-form-item label="司机ID" prop="driverId">
          <el-input v-model="form.driverId" placeholder="请输入司机ID" />
        </el-form-item>
        <el-form-item label="车辆ID" prop="carId">
          <el-input v-model="form.carId" placeholder="请输入车辆ID" />
        </el-form-item>
        <el-form-item label="总里程" prop="totalMileage">
          <el-input v-model="form.totalMileage" placeholder="请输入总里程" />
        </el-form-item>
        <el-form-item label="预估金额" prop="estimatedAmount">
          <el-input v-model="form.estimatedAmount" placeholder="请输入预估金额" />
        </el-form-item>
        <el-form-item label="开始时间" prop="startTime">
          <el-date-picker clearable
            v-model="form.startTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="endTime">
          <el-date-picker clearable
            v-model="form.endTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="订单总金额" prop="totalAmount">
          <el-input v-model="form.totalAmount" placeholder="请输入订单总金额" />
        </el-form-item>
        <el-form-item label="实付金额" prop="actualPayment">
          <el-input v-model="form.actualPayment" placeholder="请输入实付金额" />
        </el-form-item>
        <el-form-item label="起点" prop="startPoint">
          <el-input v-model="form.startPoint" placeholder="请输入起点" />
        </el-form-item>
        <el-form-item label="终点" prop="endPoint">
          <el-input v-model="form.endPoint" placeholder="请输入终点" />
        </el-form-item>
        <el-form-item label="用户优惠券ID" prop="userCouponId">
          <el-input v-model="form.userCouponId" placeholder="请输入用户优惠券ID" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 订单详情 -->
    <el-dialog title="订单详情" :visible.sync="openDetails" width="1000px" append-to-body>
  <el-form ref="Details" :model="Details" label-width="100px">
    <el-row>
      <el-col :span="8">
        <el-form-item label="用户ID" prop="userId">
          <el-input v-model="Details.userId" :disabled="true" v-if="Details.userId!== null" />
        </el-form-item>
      </el-col>
      <el-col :span="8">
        <el-form-item label="用户姓名" prop="tuserDo.userName">
          <el-input v-model="Details.tuserDo.userName" :disabled="true" v-if="Details.tuserDo!== null" />
        </el-form-item>
      </el-col>
      <el-col :span="8">
        <el-form-item label="用户手机" prop="tuserDo.userPhone">
          <el-input v-model="Details.tuserDo.userPhone" :disabled="true" v-if="Details.tuserDo!== null" />
        </el-form-item>
      </el-col>
    </el-row>

    <el-row>
      <el-col :span="8">
        <el-form-item label="司机ID" prop="driverId">
          <el-input v-model="Details.driverId" :disabled="true" v-if="Details.driverId!== null" />
        </el-form-item>
      </el-col>
      <el-col :span="8">
        <el-form-item label="司机姓名" prop="tdriverDo.driverName">
          <el-input v-model="Details.tdriverDo.driverName" :disabled="true" v-if="Details.tdriverDo!== null" />
        </el-form-item>
      </el-col>
      <el-col :span="8">
        <el-form-item label="司机手机" prop="tdriverDo.driverPhone">
          <el-input v-model="Details.tdriverDo.driverPhone" :disabled="true" v-if="Details.tdriverDo!== null" />
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="6">
        <el-form-item label="车辆ID" prop="carId">
          <el-input v-model="Details.carId" :disabled="true" v-if="Details.carId!== null" />
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label="品牌型号" prop="tcarDo.brandModel">
          <el-input v-model="Details.tcarDo.brandModel" :disabled="true" v-if="Details.tcarDo!== null" />
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label="车牌号" prop="tcarDo.licensePlateNumber">
          <el-input v-model="Details.tcarDo.licensePlateNumber" :disabled="true" v-if="Details.tcarDo!== null" />
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label="出车类型" prop="tcarDo.tcarTypeDo.typeName">
          <el-input v-model="Details.tcarDo.tcarTypeDo.typeName" :disabled="true" v-if="Details.tcarDo!== null && Details.tcarDo.tcarTypeDo!== null" />
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="8">
        <el-form-item label="预估金额" prop="estimatedAmount">
          <el-input v-model="Details.estimatedAmount" :disabled="true" v-if="Details.estimatedAmount!== null" />
        </el-form-item>
      </el-col>
      <el-col :span="8">
        <el-form-item label="实付金额" prop="actualPayment">
          <el-input v-model="Details.actualPayment" :disabled="true" v-if="Details.actualPayment!== null" />
        </el-form-item>
      </el-col>
      <el-col :span="8">
        <el-form-item label="订单总金额" prop="totalAmount">
          <el-input v-model="Details.totalAmount" :disabled="true" v-if="Details.totalAmount!== null" />
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="8">
        <el-form-item label="开始时间" prop="startTime">
          <el-input v-model="Details.startTime" :disabled="true" v-if="Details.startTime!== null" />
        </el-form-item>
      </el-col>
      <el-col :span="8">
        <el-form-item label="结束时间" prop="endTime">
          <el-input v-model="Details.endTime" :disabled="true" v-if="Details.endTime!== null" />
        </el-form-item>
      </el-col>
      <el-col :span="8">
        <el-form-item label="总里程" prop="totalMileage">
          <el-input v-model="Details.totalMileage" :disabled="true" v-if="Details.totalMileage!== null" />
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="8">
        <el-form-item label="起点" prop="startPoint">
          <el-input v-model="Details.startPoint" :disabled="true" v-if="Details.startPoint!== null" />
        </el-form-item>
      </el-col>
      <el-col :span="8">
        <el-form-item label="终点" prop="endPoint">
          <el-input v-model="Details.endPoint" :disabled="true" v-if="Details.endPoint!== null" />
        </el-form-item>
      </el-col>
      <el-col :span="8">
        <el-form-item label="用户优惠券ID" prop="userCouponId">
          <el-input v-model="Details.userCouponId" :disabled="true" v-if="Details.userCouponId!== null" />
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="12">
        <el-form-item label="司机佣金" prop="tcommissionDo.income">
          <el-input v-model="Details.tcommissionDo.income" :disabled="true" v-if="Details.tcommissionDo!== null" />
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="平台盈利" prop="tplatformRevenueDo.revenueAmount">
          <el-input v-model="Details.tplatformRevenueDo.revenueAmount" :disabled="true" v-if="Details.tplatformRevenueDo!== null" />
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="4">
        <el-form-item label="订单评分" prop="tcommentDo.commentScore">
          <el-input v-model="Details.tcommentDo.commentScore" :disabled="true" v-if="Details.tcommentDo!== null" />
        </el-form-item>
      </el-col>
      <el-col :span="20">
        <el-form-item label="订单评论" prop="tcommentDo.commentText">
          <el-input v-model="Details.tcommentDo.commentText" :disabled="true" v-if="Details.tcommentDo!== null" />
        </el-form-item>
      </el-col>
    </el-row>
    <el-form-item label="行程轨迹" prop="ttrajectoryDo.trajectoryData">
      <el-input v-model="Details.ttrajectoryDo.trajectoryData" :disabled="true" v-if="Details.ttrajectoryDo!== null" />
    </el-form-item>
  </el-form>
</el-dialog>


  </div>
</template>

<script>
import { listOrder, getOrder, delOrder, addOrder, updateOrder} from "@/api/didi/order";

export default {
  dicts: ['didi_order_status'],

  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 角色表格数据
      orderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openDetails :false,
      // 日期范围
      dateRange: [],
      getOrderStatusColor(status) {
        if (status === 1 || status === 2) {
          return 'blue';
        } else if (status === 3 || status === 4) {
          return 'green';
        } else if (status === 5) {
          return 'red';
        }
        return '';
      },
      // 数据范围选项
      // dataScopeOptions: [
      //   {
      //     value: "1",
      //     label: "已下单"
      //   },
      //   {
      //     value: "2",
      //     label: "运行中"
      //   },
      //   {
      //     value: "3",
      //     label: "已完成"
      //   },
      //   {
      //     value: "4",
      //     label: "已评价"
      //   },
      //   {
      //     value: "5",
      //     label: "已取消"
      //   }
      // ],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        customerName: "",
        driverName: "",
        licensePlateNumber:"",
        actualPayment:"",
        orderStatus: ""
      },
      // 表单参数
      form: {},
      Details:{},
      // 表单校验
      rules: {
        userId: [
          { required: true, message: "客户ID不能为空", trigger: "blur" }
        ],
        driverId: [
          { required: true, message: "司机ID不能为空", trigger: "blur" }
        ],
        carId: [
          { required: true, message: "角色顺序不能为空", trigger: "blur" }
        ],
        startTime: [
          { required: true, message: "开始时间不能为空", trigger: "blur" }
        ],
        startPoint: [
          { required: true, message: "起点不能为空", trigger: "blur" }
        ],
        endPoint: [
          { required: true, message: "终点不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.reset();
  },
  methods: {
    /** 查询角色列表 */
    getList() {
      this.loading = true;
      listOrder(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.orderList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    OrderDetails(row){
      this.Details=row
      this.openDetails=!this.openDetails

    },

    // 订单状态修改
    handleOrderStatusChange(row) {
      this.form.id=row.id
      this.form.orderStatus=5
      this.$modal.confirm('确认要终止"' + row.tuserDo.userName + '"的订单吗？').then(() =>  {
         return updateOrder(this.form);
      }).then(() => {
        this.$modal.msgSuccess("修改成功");
              this.getList();
      })
    },
    //对应订单状态
    // getOrderStatusLabel(status) {
    //   const option = this.dataScopeOptions.find(item => item.value === status);
    //   return option ? option.label : status;
    // },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
     // 表单重置
     reset() {
      this.form = {
        id: null,
        userId: null,
        driverId: null,
        carId: null,
        totalMileage: null,
        estimatedAmount: null,
        startTime: null,
        endTime: null,
        orderStatus: null,
        totalAmount: null,
        actualPayment: null,
        startPoint: null,
        endPoint: null,
        userCouponId: null
      };
      this.Details={
        actualPayment:null,
        carId:null,
        driverId:null,
        endPoint:null,
        endTime:null,
        estimatedAmount:null,
        id:null,
        orderStatus:null,
        startPoint:null,
        startTime:null,
        tcarDo:{
          brandModel:null,
          licensePlateNumber:null,
          tcarTypeDo:{
            typeName:null,
          }
        },
        tdriverDo:{
          driverName:null,
          driverPhone:null,
        },
        totalAmount:null,
        totalMileage:null,
        tuserDo:{
          userName:null,
          userPhone:null,
        },
        userCouponId:null,
        userId:null,
        tcommentDo:{
          commentScore:null,
          commentText:null,
        },
        tcommissionDo:{
          income:null
        },
        tplatformRevenueDo:{
          revenueAmount:null
        },
        ttrajectoryDo:{
          trajectoryData:null
        }

      }
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加订单";
    },
    /** 修改按钮操作 */
    handleUpdate() {
      this.reset();
      const id = this.ids[0]
      getOrder(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改订单";
      });
    },
    /** 选择角色权限范围触发 */

    dataScopeSelectChange(value) {
      this.queryParams.orderStatus=value
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateOrder(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOrder(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete() {
      const ids = this.ids;
      this.$modal.confirm('是否确认删除订单数据？').then(() => {
        return delOrder(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      })
    },

  }
};
</script>
